package com.fruit.util.sms.code;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;

import com.fruit.pojo.sms.SmsTemplet;
import com.fruit.util.CustomUtil;
import com.fruit.util.HttpClientManagerT;

/**
 * 网易云信
 * @author Administrator
 *
 */
public class NeteaseSms {
	

	
	public static String newPost(SmsTemplet st, List<NameValuePair> nvps){
		String result = null;
		HttpPost httpPost = new HttpPost(st.getUrl());
		String nonce = String.valueOf(CustomUtil.buildRandom(6));
		String curTime = String.valueOf((new Date()).getTime() / 1000L);//time
		httpPost.addHeader("AppKey", st.getAppKey());
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        String checkSum = CheckSumBuilder.getCheckSum(st.getAppSecret(), nonce ,curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        try{
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        result = HttpClientManagerT.postData(httpPost);
        }catch(Exception e){
        	result = "exception";
        }
        return result;
        
	}
	
	public static void main(String[] args) {
	/*	String result = null;
		HttpPost httpPost = new HttpPost("https://free-api.heweather.com/s6/weather");
		httpPost.addHeader("location", "北京");
		httpPost.addHeader("key", "30accb2e674c441d8ad8700e649157ee");
        httpPost.addHeader("lang", "zh");
        httpPost.addHeader("unit", "m");
       // httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        try{
        result = HttpClientManagerT.postData(httpPost);
        System.out.println(result);
        }catch(Exception e){
        	result = "exception";
        }*/
        
        String param = "key=30accb2e674c441d8ad8700e649157ee&location=邯郸&lang=zh&unit=m";
        StringBuilder sb = new StringBuilder();
        InputStream    is=null;
        BufferedReader br=null;
        try {
            //接口地址
            String url = "https://free-api.heweather.com/s6/weather/now";
            URL uri = new URL(url);
            HttpURLConnection connection= (HttpURLConnection) uri.openConnection();
            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(10000);
            connection.setRequestProperty("accept", "*/*");
            //发送参数
            connection.setDoOutput(true);
            PrintWriter out = new PrintWriter(connection.getOutputStream());
            out.print(param);
            out.flush();
            //接收结果
            is = connection.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            String         line;
            //缓冲逐行读取
            while ( (line = br.readLine()) != null ) {
                sb.append(line);
            }
        }catch ( Exception ignored ){}
        finally {
            //输出结果
            System.out.println(sb.toString());
            
        }
	}
	

	public static void batchSms(final NeteaseObj sms){
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < sms.getMobile().length; i++) {
					//sendTemplateSms(sms.getTemplateId(), sms.getMobile()[i], (sms.getParams()[i]).toString());
				}
			}
		}).start();
	}

	
}
